<template>
  <div class="user-content">
    <!-- 顶部 -->
    <div class="header">
      <el-button type="primary" @click="exportTable">查看声明</el-button>
      <el-input
        v-model="keyword"
        placeholder="请输入用户信息"
        class="ipt"
      ></el-input>
      <el-button type="primary" @click="toSearch">搜索</el-button>
      <el-button type="primary" @click="addUser">添加新用户</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="userInfoList" stripe style="width: 100%" border>
      <el-table-column align="center" prop="id" label="ID" width="80">
      </el-table-column>
      <el-table-column align="center" prop="username" label="用户名">
      </el-table-column>
      <el-table-column align="center" prop="password" label="密码">
      </el-table-column>
      <el-table-column align="center" prop="email" label="邮箱">
      </el-table-column>
      <el-table-column align="center" label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.$index, scope.row)"
            style="margin-right: 10px"
            >编辑</el-button
          >
          <el-popconfirm
            title="确定删除该用户吗？"
            @confirm="handlerDelete(scope.$index, scope.row)"
          >
            <el-button size="mini" type="danger" slot="reference">
              删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <Pagination :total="total" :pageSize="pageSize" />
    <!-- 弹出框 -->
    <UserDialog ref="userDialog" :rowUserData="rowUserData" :title="title"/>
    <!-- pdf弹出框 -->
    <PdfDialog ref="pdfDialog"/>
  </div>
</template>

<script>
import { getUserList, getUserSearch, deleteUser } from "@/api";
import Pagination from "@/components/Pagination";
import UserDialog from "./UserDialog";
import PdfDialog from "./PdfDialog";
export default {
  components: {
    Pagination,
    UserDialog,
    PdfDialog
  },
  data() {
    return {
      userInfoList: [],
      total: 0,
      pageSize: 1,
      keyword: "",
      type: 1,
      currentPage: 1,
      searchList: [],
      rowUserData: {},
      title: '添加用户'
    };
  },
  mounted() {
    this.userList();
  },
  methods: {
    async userList(page) {
      let res = await getUserList({page});
      if (res.status === 200) {
        this.userInfoList = res.data;
        this.total = res.total;
        this.pageSize = res.pageSize;
        this.type = 1;
      }
    },
    getPageNum(page) {
      // 让当前点击页码高亮
      this.currentPage = page;
      if (this.type === 1) {
        // 所有商品分页
        this.userList(page);
      } else {
        // 搜索商品分页
        this.userInfoList = this.searchList.slice((page - 1) * 7, page * 7);
      }
    },
    async toSearch() {
      this.currentPage = 1;
      let { keyword } = this;
      if (!keyword) {
        // 输入关键字为空
        this.userList(1);
        return false;
      }
      let res = await getUserSearch({ search: keyword });
      if (res.status === 200) {
        this.searchList = res.result;
        this.userInfoList = res.result.slice(0, 7);
        this.total = res.result.length; //查询数目总条数
        this.pageSize = 7;
        this.type = 2;
      } else {
        // 无数据处理
        this.userInfoList = [];
        this.total = 0;
        this.pageSize = 1;
      }
    },
    addUser() {
        this.$refs.userDialog.dialogVisible = true;
    },
    handleEdit(index, row) {
        this.$refs.userDialog.dialogVisible = true;
        this.rowUserData = {...row};
        this.title = '编辑用户信息';
    },
    async handlerDelete(index, row) {
        let {id} = row;
        let res = await deleteUser({id});
        if (res.status === 200) {
            this.$message({
                type: 'success',
                message: '删除成功'
            });
            this.userList(1);
        } else {
            this.$message({
                type: 'error',
                message: '删除失败'
            });
        }
    },
    exportTable() {
      this.$refs.pdfDialog.dialogVisible = true;
    }
  },
};
</script>

<style lang="less" scoped>
.user-content {
  padding: 20px;
  .header {
    display: flex;
    margin-bottom: 20px;
    .ipt {
      margin: 0 10px;
    }
  }
}
</style>
